package cn.chencq.moudle.proxyip.site.impl;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.chencq.moudle.proxyip.entity.ProxyIp;
import cn.chencq.moudle.proxyip.site.PageCrawlerService;

@Service
public class PageCrawlerServiceImpl implements PageCrawlerService {

	private static final Logger logger = LoggerFactory.getLogger(PageCrawlerServiceImpl.class);

	@Override
	public String crawlerHtml(String url, ProxyIp proxyIp) {
		Connection con = Jsoup.connect(url);
		if (Objects.nonNull(proxyIp)) {
			con.proxy(proxyIp.getIp(), Integer.parseInt(proxyIp.getPort()));
		}

		String host = getHost(url);
		con.header("Host", host);

		con.header("User-Agent",
				"Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.1599.103 Mobile Safari/537.36");
		String res = null;
		try {
			res = con.ignoreContentType(true).timeout(5000).get().html();
		} catch (Exception e) {
			logger.error("request throw exception", e);
		}

		return res;
	}

	public String getHost(String url) {
		String host = "";
		Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
		Matcher matcher = p.matcher(url);
		if (matcher.find()) {
			host = matcher.group();
		}
		return host;
	}

	@Override
	public String crawlerData(String url, ProxyIp proxyIp) {
		Connection con = Jsoup.connect(url);
		if (Objects.nonNull(proxyIp)) {
			con.proxy(proxyIp.getIp(), Integer.parseInt(proxyIp.getPort()));
		}

		String host = getHost(url);
		con.header("Host", host);

		con.header("User-Agent",
				"Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.1599.103 Mobile Safari/537.36");
		String res = null;
		try {
			res = con.ignoreContentType(true).timeout(5000).method(Method.GET).execute().body();
		} catch (Exception e) {
			logger.error("request throw exception", e);
		}
		
		return res;
	}

}
